package icu.funkye.confine.failHandle;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author jianbin.chen
 */
public class DefaultConfineFailHandle implements ConfineFailHandle {

    ObjectMapper mapper = new ObjectMapper();

    @Override
    public void failBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> result = new HashMap<>();
        result.put("code", HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        result.put("message", "系统超负载进行限流,请重试");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache,no-store");
        response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        response.getWriter().println(mapper.writeValueAsString(result));
    }

}
